using System;
using System.Collections.Generic;
using System.Text;
using DCI6.Client.API;
using DCI6.Client.Payloads1112.asasii;

namespace DCIAPIRMSampleCode {
    public class DCIReceiver : DCIMessageListener {
        #region DCIMessageListener Members

        public void dciReadvertisementNotificationReceived(string origID, string newID, string ddsID) {
            Console.WriteLine("dciReadvertisementNotificationReceived - origID=[{0}], newID=[{1}], and ddsID=[{2}]",
                origID, newID, ddsID);
        }

        public void dciResubsciptionNotificationReceived(string origID, string newID, string ddsID) {
            Console.WriteLine("dciResubsciptionNotificationReceived - origID=[{0}], newID=[{1}], and ddsID=[{2}]",
                origID, newID, ddsID);
        }

        public void dciStartupNotificationReceived(string timeStamp) {
            Console.WriteLine("dciStartupNotificationReceived - timeStamp=[{0}]", timeStamp);
        }

        public void dciShutdownNotificationReceived(string timeStamp) {
            Console.WriteLine("dciShutdownNotificationReceived - timeStamp=[{0}]", timeStamp);
        }

        public void validPublicationReceived(List<Item> items, string ddsID) {
            Console.WriteLine("validPublicationReceived - Recieved [{0}] Items, ddsID=[{1}]", items.Count, ddsID);
            foreach (Item i in items) {
                Console.WriteLine("Item [{0}] XML: {1}", i.ItemID, i.ItemXml);
                Console.WriteLine("DCI Meta Data:");
                Console.WriteLine("AdvertisementID [{0}] ", i.AdvertisementID);
                Console.WriteLine("DDS CallbackType [{0}] ", i.CallbackType.ToString());
                Console.WriteLine("Invalid MSG [{0}] ", i.InvalidMessage);
                Console.WriteLine("Valid To AOI [{0}]", i.IsValidToAOI);
                Console.WriteLine("Valid To Schema [{0}]", i.IsValidToSchema);

               
                if (i.IsValidQueryId) {
                    Console.WriteLine("Query ID [{0}] ", i.QueryId);
                }

                if (i.IsValidSubscriptionId) {
                    Console.WriteLine("Subscription ID [{0}] ", i.SubscriptionId);
                }
                
                #region HOW TO USE THE PAYLOADS LIBRARIES FOR POSITION REPORTS

                String xml = i.DDSItem.ItemData.AnyElement[0].ToXml();

                if (i.RootTag == "PositionReport") {
                    try {
                        DCI6.Client.Payloads1112.abcs.PositionReport p1112 = new DCI6.Client.Payloads1112.abcs.PositionReport();
                        p1112.FromXml(xml);
                        Console.WriteLine("LAT: " + p1112.Latitude);
                        Console.WriteLine("LON: " + p1112.Longitude);
                        if (p1112.PositionReport_Choice.Unit != null) {
                            Console.WriteLine("Unit name=" + p1112.PositionReport_Choice.Unit.Name);
                            Console.WriteLine("Unit URN=" + p1112.PositionReport_Choice.Unit.URN);
                        }
                        else if (p1112.PositionReport_Choice.Platform != null) {
                            Console.WriteLine("Platform name=" + p1112.PositionReport_Choice.Platform.Name);
                            Console.WriteLine("Platform URN=" + p1112.PositionReport_Choice.Platform.URN);
                        }
                        Console.WriteLine("validPublicationReceived 11/12 POS-RPT received");
                    }
                    catch (Exception e) {
                        Console.WriteLine("11/12 " + e.Message);
                    }


                    try {
                        DCI6.Client.Payloads1314.posrpt.PositionReport p1314 = new DCI6.Client.Payloads1314.posrpt.PositionReport();
                        p1314.FromXml(xml);
                        Console.WriteLine("LAT: " + p1314.Location.Latitude);
                        Console.WriteLine("LON: " + p1314.Location.Longitude);
                        if (p1314.PositionReport_Type_Choice.Unit != null) {
                            Console.WriteLine("Unit name=" + p1314.PositionReport_Type_Choice.Unit.Name);
                            Console.WriteLine("Unit URN=" + p1314.PositionReport_Type_Choice.Unit.URN);
                        }
                        else if (p1314.PositionReport_Type_Choice.Platform != null) {
                            Console.WriteLine("Platform name=" + p1314.PositionReport_Type_Choice.Platform.Name);
                            Console.WriteLine("Platform URN=" + p1314.PositionReport_Type_Choice.Platform.URN);
                        }
                        Console.WriteLine("validPublicationReceived 13/14 POS-RPT received");
                    }
                    catch (Exception e) {
                        Console.WriteLine("13/14 " + e.Message);
                    }
                }

                #endregion
            }
        }

        public void invalidPublicationReceived(List<Item> items, string ddsID) {
            Console.WriteLine("invalidPublicationReceived - Recieved [{0}] Items, ddsID=[{1}]", items.Count, ddsID);
        }

        public void retractItemNotificationReceived(string advertisementId, string itemId, string ddsID) {
            Console.WriteLine("retractItemNotificationReceived - advertisementID=[{0}], itemID=[{1}], and ddsID=[{2}]",
                advertisementId, itemId, ddsID);
        }

        public void DDSConnectionStatusChanged(bool connectionFlag, string ddsID) {
            Console.WriteLine("DDSConnectionStatusChanged - connectedFlag=[{0}], ddsID=[{1}]", connectionFlag, ddsID);
        }

        public void validQueryPublicationReceived(string queryID, bool complete, List<Item> items, string ddsID) {
            Console.WriteLine(
                "validQueryPublicationReceived - Recieved [{0}] Items, queryID=[{1}], complete=[{2}], ddsID=[{3}]",
                items.Count, queryID, complete, ddsID);
        }

        public void invalidQueryPublicationReceived(string queryID, bool complete, List<Item> items, string ddsID) {
            Console.WriteLine(
                "invalidQueryPublicationReceived - Recieved [{0}] Items, queryID=[{1}], complete=[{2}], ddsID=[{3}]",
                items.Count, queryID, complete, ddsID);
        }

        #endregion
    }
}